package com.fausto.learnimprint.core.model.dto

import com.fausto.learnimprint.common.enum.ResultCodeEnum
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty


@ApiModel("统一返回结果")
data class CommonResult<T>(

    @ApiModelProperty("返回码")
    var code: Int,

    @ApiModelProperty("返回消息")
    var message: String,

    @ApiModelProperty("返回数据")
    var data: T?
) {
    constructor(_data: T?) : this(0, "无消息", _data)

    companion object {
//        fun <T> build(data: T): CommonResult<T> {
//            return CommonResult(data)
//        }

        fun <T> build(body: T?, resultCodeEnum: ResultCodeEnum): CommonResult<T> {
            return CommonResult(body).apply {
                this.code = resultCodeEnum.code
                this.message = resultCodeEnum.message
            }
        }

//        fun <T> build(code: Int, message: String): CommonResult<T> {
//            return CommonResult(code, message, null)
//        }

        fun <T> ok(msg: String, data: T? = null): CommonResult<T> {
            return CommonResult(
                ResultCodeEnum.SUCCESS.code,
                msg,
                data
            )
        }

        fun <T> fail(msg: String, data: T? = null): CommonResult<T> {
            return CommonResult(
                ResultCodeEnum.FAIL.code,
                msg,
                data
            )
        }
    }


    fun message(msg: String): CommonResult<T> {
        return CommonResult(0, msg, null)
    }

    fun code(code: Int): CommonResult<T> {
        return CommonResult(code, "无消息", null)
    }

    fun isOk(): Boolean {
        return this.code == ResultCodeEnum.SUCCESS.code
    }
}